package sort;
/**
 * @Auther: wangliuyang
 * @Date: 2021/4/23 19:21
 * @Description: 选择排序
 */
public class SelectionSort {
    public static void selectionSort(int[] nums) {
        for (int i = 0; i < nums.length - 1; i++) {
            // 最小值在哪个位置上  i～n-1
            int maxIndex = i;
            for (int j = 0; j < nums.length - i ; j++) {
                if (nums[j] > nums[maxIndex] ) {
                    maxIndex = j;
                }
            }
            swap(nums,nums.length - i - 1, maxIndex);
        }
    }

    private static void swap(int[] nums, int i, int j) {
        int tmp = nums[i];
        nums[i] = nums[j];
        nums[j] = tmp;
    }

    public static void main(String[] args) {
        int[] array = new int[]{2,5,6,7,3,7,1};
        selectionSort(array); ;
        for (int i : array) {
            System.out.print(i + " ");
        }
    }
}
